//
//  SASaleDocumentsCell.m
//  saasRetail
//
//  Created by 黄志刚 on 2021/5/11.
//

#import "SASaleDocumentsCell.h"
#import "SAInvoiceListModel.h"
#import "SAWarehousingDocumentsModel.h"
#import "SAPayDocumentsModel.h"
#import "SASettlementDocumentsModel.h"
#import "UILabel+NSMutableAttributedString.h"

@interface SASaleDocumentsCell ()

@property (nonatomic, strong) UILabel *titleLab;
@property (nonatomic, strong) UILabel *numLab;
@property (nonatomic, strong) UILabel *priceLab;
@property (nonatomic, strong) UIView *line;
@property (nonatomic, strong) UIImageView *imageV;
@property (nonatomic, strong) UIImageView *saleImageV;

@property (nonatomic, strong) UILabel *dateLab;
@property (nonatomic, strong) UILabel *orderNoLab;

@end

@implementation SASaleDocumentsCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if(self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]){
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self initUI];
    }
    return self;
}

-(void)setModel:(SAInvoiceListModel *)model {
    _model = model;
    if (model.memberName.length > 0) {
        self.titleLab.text = model.memberName;
    }else {
        self.titleLab.text = @"散客";
    }
    self.dateLab.text = model.salesTime;
    self.orderNoLab.text = model.salesOrderNo;
    
    if ([model.totalCount floatValue] < 0) {
        self.numLab.textColor = kPinkColor;
    }else {
        self.numLab.textColor = kBlackColor;
    }
    self.numLab.text = model.totalCount;
    if ([model.actualPrice floatValue] < 0) {
        self.priceLab.textColor = kPinkColor;
    }else {
        self.priceLab.textColor = kBlackColor;
    }

    self.priceLab.text = [NSString stringWithFormat:@"￥%0.2f",[model.actualPrice doubleValue]/100.f];
    if ([model.returnTotalCount doubleValue] == 0) {
        self.saleImageV.hidden = YES;
    } else if ([model.returnTotalCount doubleValue] == [model.totalCount doubleValue]) {
        self.saleImageV.hidden = NO;
        self.saleImageV.image = [UIImage imageNamed:@"sale_refunded"];
    } else if ([model.returnTotalCount doubleValue] != [model.totalCount doubleValue]) {
        self.saleImageV.hidden = NO;
        self.saleImageV.image = [UIImage imageNamed:@"sale_PartialRetreat"];
    }
}

-(void)setDocumentsModel:(SAWarehousingDocumentsModel *)documentsModel {
    _documentsModel = documentsModel;
    if (documentsModel.providerName.length > 0) {
        self.titleLab.text = documentsModel.providerName;
        NSMutableArray *menuList = [SAValueManger shareInStance].userInfo.menuList;
        NSString *isSupplier = [NSString stringWithFormat:@"%@",menuList[7][@"isDelete"]];
        NSString *seeProvider = [NSString stringWithFormat:@"%@",menuList[7][@"perms"][@"seeProvider"]];
        if ([isSupplier isEqualToString:@"1"]) {
            self.titleLab.text = @"******";
        }
        if ([seeProvider isEqualToString:@"1"]) {
            self.titleLab.text = @"******";
        }
    }else {
        self.titleLab.text = @"零散供货商";
    }
    self.dateLab.text = documentsModel.dayTime;
    self.orderNoLab.text = documentsModel.purchaseOrderNo;
    
    if ([documentsModel.count floatValue] < 0) {
        self.numLab.textColor = kPinkColor;
    }else {
        self.numLab.textColor = kBlackColor;
    }
    self.numLab.text = documentsModel.count;
    
    if ([documentsModel.price floatValue] < 0) {
        self.priceLab.textColor = kPinkColor;
    }else {
        self.priceLab.textColor = kBlackColor;
    }
    self.priceLab.text = [NSString stringWithFormat:@"￥%0.2f",[documentsModel.price doubleValue]/100.f];
    
    NSString *productViewCostPrice = [NSString stringWithFormat:@"%@",[SAValueManger shareInStance].userInfo.menuList[1][@"perms"][@"productViewCostPrice"]];
    if ([productViewCostPrice isEqualToString:@"1"]) {
        self.priceLab.text = @"￥******";
    }
}

-(void)setPayModel:(SAPayDocumentsModel *)payModel {
    _payModel = payModel;
    if (payModel.memberName.length > 0) {
        self.titleLab.text = payModel.memberName;
    }else {
        self.titleLab.text = @"散客";
    }
//    self.dateLab.text = payModel.dayTime;
    self.orderNoLab.text = payModel.receiveOrderNo;
    
    if ([payModel.amount floatValue] < 0) {
        self.priceLab.textColor = kPinkColor;
    }else {
        self.priceLab.textColor = kBlackColor;
    }
    self.priceLab.text = [NSString stringWithFormat:@"￥%0.2f",[payModel.amount doubleValue]/100.f];
}

-(void)setSettlementModel:(SASettlementDocumentsModel *)settlementModel {
    _settlementModel = settlementModel;
    self.titleLab.text = settlementModel.settlementDate;
    
    if ([settlementModel.amount floatValue] < 0) {
        self.priceLab.textColor = kPinkColor;
    }else {
        self.priceLab.textColor = kBlackColor;
    }
    self.priceLab.text = [NSString stringWithFormat:@"￥%0.2f",[settlementModel.amount doubleValue]/100.f];
    self.imageV.hidden = YES;
}

-(void)initUI {
    [self.contentView addSubview:self.dateLab];
    [self.contentView addSubview:self.titleLab];
    [self.contentView addSubview:self.orderNoLab];
    [self.contentView addSubview:self.numLab];
    [self.contentView addSubview:self.priceLab];
    [self.contentView addSubview:self.line];
    [self.contentView addSubview:self.imageV];
    [self.contentView addSubview:self.saleImageV];
    
    [self.priceLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);
        make.right.mas_equalTo(kScaleNum(-40));
        make.height.mas_equalTo(kScaleNum(70));
    }];
    
    [self.saleImageV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.priceLab.mas_left);
        make.centerY.equalTo(self.priceLab);
        make.height.width.mas_equalTo(kScaleNum(40));
    }];
}

-(UILabel *)dateLab {
    if (!_dateLab) {
        _dateLab = [[UILabel alloc] initWithFrame:SARectMake(25, 5, 135, 20)];
        _dateLab.textColor = kBlackColor;
        _dateLab.font = kFont(15);
    }
    return _dateLab;
}

-(UILabel *)titleLab {
    if (!_titleLab) {
        _titleLab = [[UILabel alloc] initWithFrame:SARectMake(25, 25, 135, 20)];
        _titleLab.textColor = kBlackColor;
        _titleLab.font = kFont(15);
    }
    return _titleLab;
}

-(UILabel *)orderNoLab {
    if (!_orderNoLab) {
        _orderNoLab = [[UILabel alloc] initWithFrame:SARectMake(25, 45, 135, 20)];
        _orderNoLab.textColor = kBlackColor;
        _orderNoLab.font = kFont(15);
    }
    return _orderNoLab;
}

-(UILabel *)numLab {
    if (!_numLab) {
        _numLab = [[UILabel alloc] initWithFrame:SARectMake(155, 0, 64, 70)];
        _numLab.textColor = kBlackColor;
        _numLab.font = kFont(15);
        _numLab.textAlignment = NSTextAlignmentCenter;
    }
    return _numLab;
}

-(UILabel *)priceLab {
    if (!_priceLab) {
        _priceLab = [[UILabel alloc] initWithFrame:CGRectZero];
        _priceLab.textColor = kBlackColor;
        _priceLab.font = kFont(15);
    }
    return _priceLab;
}

-(UIImageView *)saleImageV {
    if (!_saleImageV) {
        _saleImageV = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"sale_PartialRetreat"]];
        _saleImageV.hidden = YES;
    }
    return _saleImageV;
}

-(UIView *)line {
    if (!_line) {
        _line = [[UIView alloc] initWithFrame:CGRectMake(0, kScaleNum(70), kScreenWidth, kScaleNum(1))];
        _line.backgroundColor = kLineColor;
    }
    return _line;
}

-(UIImageView *)imageV {
    if (!_imageV) {
        _imageV = [[UIImageView alloc] initWithFrame:CGRectMake(kScreenWidth - kScaleNum(35), kScaleNum(24), kScaleNum(22), kScaleNum(22))];
        _imageV.image = [UIImage imageNamed:@"Sale_rightArrow2"];
    }
    return _imageV;
}

@end
